//
//  JFBCompleteDS.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBCompleteOrderDS.h"
#import "JFBOrderDS.h"
#import "JFBDailyDS.h"

@implementation JFBCompleteOrderDS

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super initWithAttributes:attributes];
    if (!self) {
        return nil;
    }
    return self;
}


- (void)buildWithMockAttributes {
    self.ID = 13;
    self.orderCode = @"8267387867";
    self.address = @"都会国际3J";
    self.requireTime = @"30分钟内送达";
    self.payment = @"微信支付";
    self.paymentOrigin = 1;
    self.totalCount = 3;
    self.money = [NSDecimalNumber decimalNumberWithString:@"58.3"];
    self.createAt = @"2015-12-3 12:04";
    self.userName = @"hello world";
    self.staff = @"小piu~";
    self.staffMobile = @"18767654389";
    self.remark = @"姑娘，带包黄鹤楼";
    
    self.completeAt = @"2015-11-12";
}

+ (NSURLSessionDataTask *)loadOrderList:(void (^)(NSArray *posts, NSError *error))block pager:(JFBPager*)pager{
    
    NSDictionary * parameter = @{@"so_status":[NSNumber numberWithInt:5]};
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/so/list.json" parameters:parameter success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *response = [JSON valueForKeyPath:@"data"];
            for( NSDictionary *order in response ) {
                [result addObject:[[JFBCompleteOrderDS alloc] initWithAttributes:order]];
            }
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updateAt" ascending:NO];
            [result sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
    
    return nil;
}

+ (NSURLSessionDataTask *)verifyIdentifyCode:(void (^)(BOOL isSuccess, JFBOrderDS *order, NSString *message, NSError *error))block orderID:(NSUInteger)orderID identifyCode:(NSString*)identifyCode {
    
    
    NSDictionary *parameter = @{@"so_id":[NSNumber numberWithInteger:orderID], @"v_code":identifyCode};
    
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/so/finish.json" parameters:parameter success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            block(true, nil, @"操作成功", nil);
        } else {
            block(false, nil, @"收货码错误", nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(false, nil, @"操作失败，请稍候重试或致电客服",  error);
    }];
    
    return nil;
}

+ (NSURLSessionDataTask *)loadDayDetailList:(void (^)(NSArray *orders, NSError *error))block so_status: (NSUInteger)orderType day: (NSString *)day
{
    NSDictionary * parameter = @{@"so_status":[NSNumber numberWithInt:5], @"day": day};
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/balance/day/list.json" parameters:parameter success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *response = [JSON valueForKeyPath:@"data"];
            for( NSDictionary *order in response ) {
                [result addObject:[[JFBCompleteOrderDS alloc] initWithAttributes:order]];
            }
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"completeAt" ascending:NO];
            [result sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}
@end
